package com.ezjie.ielts.util;

import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.text.Html;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.text.style.StyleSpan;

public class TextUtil {
	/**
	 * 转化标题 分为2个颜色显示
	 * 
	 * @param string
	 * @return
	 */
	public static SpannableString transferTitle(String string) {
		SpannableString sp = new SpannableString(string);
		if (string.length() >= 2) {
			int end = string.length() - 2;
			sp.setSpan(new ForegroundColorSpan(Color.rgb(255, 178, 0)), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return sp;
	}

	/**
	 * 转化标题 分为2个颜色显示
	 * 
	 * @param context
	 * @param resId
	 *            可以传入一个资源id R.string.hello
	 * @return
	 */
	public static SpannableString transferTitle(Context context, int resId) {
		String string = context.getResources().getString(resId);
		SpannableString sp = new SpannableString(string);
		if (string.length() >= 2) {
			int end = string.length() - 2;
			sp.setSpan(new ForegroundColorSpan(Color.rgb(255, 178, 0)), 0, end, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
		}
		return sp;
	}

	/**
	 * 把某个关键词 变成黑色 粗体
	 * 
	 * @param keyword
	 *            关键字
	 * @param origin
	 *            例句
	 * @return
	 */
	public static SpannableString transferBoldWord(String keyword, String origin) {
		if (origin == null) {
			origin = "";
		}
		Spanned exampleSpanned = Html.fromHtml(origin);

		return transferBoldWord(keyword, exampleSpanned);
	}

	public static SpannableString transferBoldWord(String keyword, Spanned exampleSpanned) {
		int indexOf = -1;
		if (!TextUtils.isEmpty(keyword)) {
			String origin = exampleSpanned.toString();
			indexOf = (origin.toUpperCase()).indexOf(keyword.toUpperCase());
		}
		SpannableString ss = new SpannableString(exampleSpanned);
		if (indexOf != -1) {
			ss.setSpan(new ForegroundColorSpan(Color.BLACK), indexOf, indexOf + keyword.length(),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);// 前包括，后不包括
			ss.setSpan(new StyleSpan(Typeface.BOLD), indexOf, indexOf + keyword.length(),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

		}
		return ss;
	}

	/**
	 * 把某个关键词 变成白色 粗体
	 * 
	 * @param keyword
	 *            关键字
	 * @param origin
	 *            例句
	 * @return
	 */
	public static SpannableString transferBoldWhiteWord(String keyword, String origin) {
		if (origin == null) {
			origin = "";
		}
		Spanned exampleSpanned = Html.fromHtml(origin);

		return transferBoldWhiteWord(keyword, exampleSpanned);
	}

	public static SpannableString transferBoldWhiteWord(String keyword, Spanned exampleSpanned) {
		int indexOf = -1;
		if (!TextUtils.isEmpty(keyword)) {
			String origin = exampleSpanned.toString();
			indexOf = (origin.toUpperCase()).indexOf(keyword.toUpperCase());
		}
		SpannableString ss = new SpannableString(exampleSpanned);
		if (indexOf != -1) {
			ss.setSpan(new ForegroundColorSpan(Color.WHITE), indexOf, indexOf + keyword.length(),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);// 前包括，后不包括
			ss.setSpan(new StyleSpan(Typeface.BOLD), indexOf, indexOf + keyword.length(),
					Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

		}
		return ss;
	}
}
